package org.proteored.miapeapi.xml.msi.adapter;

import java.util.List;

import org.proteored.miapeapi.cv.ms.MassToleranceUnit;
import org.proteored.miapeapi.cv.msi.AdditionalParameterName;
import org.proteored.miapeapi.cv.msi.CleavageName;
import org.proteored.miapeapi.cv.msi.SearchType;
import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.msi.AdditionalParameter;
import org.proteored.miapeapi.interfaces.msi.Database;
import org.proteored.miapeapi.interfaces.msi.InputParameter;
import org.proteored.miapeapi.xml.msi.autogenerated.MSIDatabase;
import org.proteored.miapeapi.xml.msi.autogenerated.MSIInputParameters;
import org.proteored.miapeapi.xml.msi.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.msi.autogenerated.ParamType;
import org.proteored.miapeapi.xml.msi.util.MSIControlVocabularyXmlFactory;
import org.proteored.miapeapi.xml.util.MiapeXmlUtil;

public class InputParameterAdapter implements Adapter<MSIInputParameters> {
	private final InputParameter inputParameter;
	private final ObjectFactory factory;
	private final MSIControlVocabularyXmlFactory cvFactory;

	public InputParameterAdapter(InputParameter inputParameter, ObjectFactory factory,
			MSIControlVocabularyXmlFactory cvFactory) {
		this.inputParameter = inputParameter;
		this.factory = factory;
		this.cvFactory = cvFactory;
	}

	@Override
	public MSIInputParameters adapt() {
		MSIInputParameters xmlInputParameter = factory.createMSIInputParameters();
		xmlInputParameter.setAAModif(inputParameter.getAaModif());
		xmlInputParameter.setAdditionalCleavages(inputParameter.getAdditionalCleavages());
		xmlInputParameter.setCleavageName(cvFactory.createCV(inputParameter.getCleavageName(),
				null, CleavageName.getInstance(cvFactory.getCvManager())));
		xmlInputParameter.setCleavageRules(inputParameter.getCleavageRules());
		xmlInputParameter.setFragmentMassTolerance(cvFactory.createParamUnit(
				inputParameter.getFragmentMassTolerance(),
				inputParameter.getFragmentMassToleranceUnit(),
				MassToleranceUnit.getInstance(cvFactory.getCvManager())));
		xmlInputParameter.setMinScore(inputParameter.getMinScore());
		xmlInputParameter.setMisscleavages(inputParameter.getMisscleavages());
		xmlInputParameter.setName(inputParameter.getName());
		xmlInputParameter.setNumEntries(inputParameter.getNumEntries());
		xmlInputParameter.setPMFMassTolerance(cvFactory.createParamUnit(
				inputParameter.getPmfMassTolerance(), inputParameter.getPmfMassToleranceUnit(),
				MassToleranceUnit.getInstance(cvFactory.getCvManager())));
		xmlInputParameter.setPrecursorMassTolerance(cvFactory.createParamUnit(
				inputParameter.getPrecursorMassTolerance(),
				inputParameter.getPrecursorMassToleranceUnit(),
				MassToleranceUnit.getInstance(cvFactory.getCvManager())));
		xmlInputParameter.setTaxonomy(inputParameter.getTaxonomy());
		xmlInputParameter.setScoringAlgorithm(inputParameter.getScoringAlgorithm());
		xmlInputParameter.setSearchType(cvFactory.createCV(inputParameter.getSearchType(), null,
				SearchType.getInstance(cvFactory.getCvManager())));
		xmlInputParameter.setId(MiapeXmlUtil.IdentifierPrefixes.PARAMETERS.getPrefix()
				+ inputParameter.getId());

		if (inputParameter.getSoftware() != null) {
			xmlInputParameter.setSoftwareRef(MiapeXmlUtil.IdentifierPrefixes.SOFTWARE.getPrefix()
					+ inputParameter.getSoftware().getId());
		}
		addAdditionalParameter(xmlInputParameter.getMSIAdditionalParameter());
		addDatabase(xmlInputParameter.getMSIDatabase());
		return xmlInputParameter;
	}

	private void addDatabase(List<MSIDatabase> msiDatabases) {
		if (inputParameter.getDatabases() != null) {
			for (Database database : inputParameter.getDatabases()) {
				msiDatabases.add(new DatabaseAdapter(database, factory, cvFactory).adapt());
			}
		}

	}

	private void addAdditionalParameter(List<ParamType> additionalParameters) {
		if (inputParameter.getAdditionalParameters() != null) {
			for (AdditionalParameter additionalParameter : inputParameter.getAdditionalParameters()) {
				additionalParameters.add(cvFactory.createCV(additionalParameter.getName(),
						additionalParameter.getValue(),
						AdditionalParameterName.getInstance(cvFactory.getCvManager())));
			}
		}
	}

}
